/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import java.util.ArrayList;
import BO.Transfer;
import Connection.Connect;
import Exceptions.DataAccessException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

/**
 *
 * @author Bruno Lopes
 */
public class TransferDao {

    public TransferDao() {
    }

    public static ArrayList<Transfer> availableTransfers() {

        ArrayList<Transfer> toReturn = new ArrayList<Transfer>();

        String query = "select *  from transfer where seats > 0";

        try {
            PreparedStatement pstmt = Connect.createConnection().prepareStatement(query);

            ResultSet results = pstmt.executeQuery(query);

            while (results.next()) {
                Transfer transfer = new Transfer();
                transfer.setId(results.getInt("id"));
                transfer.setSeats(results.getInt("seats"));
                transfer.setDestination(results.getString("destination"));
                transfer.setTransferType(results.getInt("transferType"));
                transfer.setTransferCompany(results.getString("transferCompany"));

                toReturn.add(transfer);
            }
        } catch (SQLException ex) {
            System.err.println("Erro: " + ex.getMessage());
        } catch (ClassNotFoundException ex) {
            System.err.println("Erro: " + ex.getMessage());
        } catch (IOException ex) {
            System.err.println("Erro: " + ex.getMessage());
        } catch (DataAccessException ex) {
            System.err.println("Erro no acesso à base de dados.");
        }

        return toReturn;
    }

    public static ArrayList<Transfer> transferListByDestination(String local) throws
            ClassNotFoundException, SQLException, FileNotFoundException,
            IOException, DataAccessException {

        String query = "select *  from transfer where destination like"
                + "'" + local + "'";

        PreparedStatement pstmt = Connect.createConnection().prepareStatement(query);

        ResultSet results = pstmt.executeQuery(query);

        ArrayList<Transfer> toReturn = new ArrayList<Transfer>();

        while (results.next()) {
            Transfer transfer = new Transfer();
            transfer.setId(results.getInt("id"));
            transfer.setSeats(results.getInt("seats"));
            transfer.setDestination(results.getString("destination"));
            transfer.setTransferType(results.getInt("transferType"));
            transfer.setTransferCompany(results.getString("transferCompany"));

            toReturn.add(transfer);
        }

        Connect.closeConnection(pstmt);
        return toReturn;

    }

    public static ArrayList<Transfer> getAll() {

        ArrayList<Transfer> toReturn = new ArrayList<Transfer>();

        String query = "select *  from hotel";

        try {
            PreparedStatement pstmt = Connect.createConnection().prepareStatement(query);

            ResultSet results = pstmt.executeQuery(query);

            while (results.next()) {
                Transfer transport = new Transfer();
                transport.setId(results.getInt("id"));
                transport.setSeats(results.getInt("seats"));
                transport.setDestination(results.getString("destination"));
                transport.setTransferCompany(results.getString("transferCompany"));
                transport.setTransferType(results.getInt("transferType"));

                toReturn.add(transport);
            }
        } catch (SQLException ex) {
            System.err.println("Erro: " + ex.getMessage());
        } catch (ClassNotFoundException ex) {
            System.err.println("Erro: " + ex.getMessage());
        } catch (IOException ex) {
            System.err.println("Erro: " + ex.getMessage());
        } catch (DataAccessException ex) {
            System.err.println("Erro no acesso à base de dados.");
        }

        return toReturn;
    }

    public static int criar(Transfer transport) throws DataAccessException,
            ClassNotFoundException, IOException {

        try {
            String query = "insert into transfer ( seats, destination, transferType, transferCompany) ";
            query += " values (?,?,?,?)";

            PreparedStatement pstmt = Connect.createConnection().prepareStatement(query);

            pstmt.setInt(1, transport.getSeats());
            pstmt.setString(2, transport.getDestination());
            pstmt.setInt(3, transport.getTransferType());
            pstmt.setString(4, transport.getTransferCompany());

            int rows = pstmt.executeUpdate(query);

            Connect.closeConnection(pstmt);

            return rows;
        } catch (ClassNotFoundException ex) {
            throw new DataAccessException("Impossivel criar ligacao JDBC. Erro no acesso a biblioteca.");
        } catch (SQLException ex) {
            throw new DataAccessException("Erro no acesso a dados. Motivo: " + ex.getMessage());
        }
    }
}
